package com.stars.system.fitness.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.stars.system.fitness.domain.FitnessPlan;
import com.stars.system.fitness.service.IFitnessPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 健身计划Controller
 * 
 * @author shuaibizhe
 * @date 2022-08-14
 */
@RestController
@RequestMapping("/fitness/plan")
public class FitnessPlanController extends BaseController
{
    @Autowired
    private IFitnessPlanService fitnessPlanService;

    /**
     * 查询健身计划列表
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(FitnessPlan fitnessPlan)
    {
        startPage();
        List<FitnessPlan> list = fitnessPlanService.selectFitnessPlanList(fitnessPlan);
        return getDataTable(list);
    }

    /**
     * 查看该周期内是否已存在健身计划
     * 存在即返回该优先级，不存在则返回0
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:list')")
    @PostMapping("/ifExistPlan")
    public int ifExistPlan(@RequestBody FitnessPlan fitnessPlan)
    {
        return fitnessPlanService.ifExistPlanInTimeScope(fitnessPlan);
    }

    /**
     * 导出健身计划列表
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:export')")
    @Log(title = "健身计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FitnessPlan fitnessPlan)
    {
        List<FitnessPlan> list = fitnessPlanService.selectFitnessPlanList(fitnessPlan);
        ExcelUtil<FitnessPlan> util = new ExcelUtil<FitnessPlan>(FitnessPlan.class);
        util.exportExcel(response, list, "健身计划数据");
    }

    /**
     * 获取健身计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") String planId)
    {
        return AjaxResult.success(fitnessPlanService.selectFitnessPlanByPlanId(planId));
    }

    /**
     * 新增健身计划
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:add')")
    @Log(title = "健身计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FitnessPlan fitnessPlan)
    {
        return toAjax(fitnessPlanService.insertFitnessPlan(fitnessPlan));
    }

    /**
     * 修改健身计划
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:edit')")
    @Log(title = "健身计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FitnessPlan fitnessPlan)
    {
        return toAjax(fitnessPlanService.updateFitnessPlan(fitnessPlan));
    }

    /**
     * 删除健身计划
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:remove')")
    @Log(title = "健身计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable String[] planIds)
    {
        return toAjax(fitnessPlanService.deleteFitnessPlanByPlanIds(planIds));
    }

    /**
     * 查询今日健身计划
     */
    @PreAuthorize("@ss.hasPermi('fitness:plan:list')")
    @GetMapping("/listTodayPlan/{type}")
    public TableDataInfo listTodayPlan(@PathVariable String type) throws NoSuchFieldException, IllegalAccessException {
//        fitness.setCreateUser(getUserId());
        List<SysDictData> list = fitnessPlanService.selectFitnessPlan(type);
        return getDataTable(list);
    }

    /**
     * 修改今日计划状态
     */
    @Log(title = "fitness", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTodayPlan")
    public void updateTodayPlan(@RequestBody SysDictData sysDictData) {
        fitnessPlanService.updateTodayPlan(sysDictData);
    }
}
